package com.glela.controller;

import com.alibaba.fastjson.JSONObject;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;
import com.glela.statistics.model.OrderQueryTotal;
import com.glela.statistics.model.request.StoreCenterRequest;
import com.glela.statistics.model.response.StoreCenterOrderResponse;
import com.glela.statistics.service.StatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 
 * @author MaoZeYao
 *
 */
@Controller
@RequestMapping("/storeCenter")
public class StoreCenterController extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(StoreCenterController.class);
	
	@Autowired
	private List<StatisticsService> statisticsServiceList;
	
	/**
	 * V4.25
	 * 订单查询
	 * @param model
	 * {"userId":152,"startTime":"","endTime":"","requestType":1,"orderSn":"","userAccount":"","queryName":"","studentId":188,"orderStatus":1,"selfSupport":0,"orderChannel":1,"pageNum":1,"pageSize":10}
	 * @return
	 */
	@RequestMapping(value = "/queryOrder111111", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryOrder(@RequestBody StoreCenterRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(),model.getOrderChannel(),model.getLoginUserId(),model.getPageNum(),model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectOrderDetailByOrderInfoIds(model)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("订单查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * @Description: 新----我的店铺订单查询（只查询在我店铺下的订单仅显示直接佣金）
	 * @Author: zhangrenjie
	 * @Date: 2018/2/3 11:20
	 *
	 */
	@RequestMapping(value = "/queryOrder", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryOrderNew(@RequestBody StoreCenterRequest model) {
		String rslt = null;
		String tempKey = null;
		try {
			if (ParameterUtils.validParams(model.getUserId(),model.getOrderChannel(),model.getLoginUserId(),model.getPageNum(),model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
//			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectOrderDetailByOrderInfoIdsNew(model)), HttpStatus.OK);
			//当查询条件为默认值时，加入缓存---2018-03-07
			if (StringUtils.isEmpty(model.getKeyWord()) &&StringUtils.isEmpty(model.getStartTime())&&StringUtils.isEmpty(model.getEndTime())&& model.getOrderStatus() == 0) {
				tempKey = getTempCacheKey(model.getUserId(),model.getStartIndex(),model.getPageSize());
				rslt = queryCacheResult(tempKey);
				//读取订单统计数量
				OrderQueryTotal total = getStatisticsService().selectOrderTotal(model);
				if (StringUtils.isEmpty(rslt)) {
//					rslt = toResultModelJSONNew(getStatisticsService().selectOrderDetailByOrderInfoIdsNew(model));
					StoreCenterOrderResponse orderResponse = getStatisticsService().selectOrderDetailByOrderInfoIdsNew(model);
					orderResponse.setOrderNum(total.getOrderNum());
					orderResponse.setGoodsNum(total.getGoodsNum());
					orderResponse.setOrderFee(total.getTotalSales());
					rslt = toResultModelJSONNew(orderResponse);
				} else {
//					return toRespEntity(rslt);
					StoreCenterOrderResponse storeCenterOrderResponse = null;
					ResponseResultModel responseResultModel = JSONObject.parseObject(rslt, ResponseResultModel.class);
					storeCenterOrderResponse = JSONObject.parseObject(responseResultModel.getData().toString(), StoreCenterOrderResponse.class);
					if (null != storeCenterOrderResponse) {
						storeCenterOrderResponse.setOrderNum(total.getOrderNum());
						storeCenterOrderResponse.setGoodsNum(total.getGoodsNum());
						storeCenterOrderResponse.setOrderFee(total.getTotalSales());
					}
					String s = JSONObject.toJSONString(new ResponseResultModel<>(storeCenterOrderResponse));
					return toRespEntity(s);
				}
			}else{//有查询条件暂不走缓存
				return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectOrderDetailByOrderInfoIdsNew(model)), HttpStatus.OK);
			}
		} catch (Exception e) {
			logger.error("订单查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
		return toRespEntity(toCacheResult(tempKey, rslt,20));//缓存20秒
	}
	
	/**
	 * V4.25
	 * 营收明细查询
	 * @param model
	 * {"userId":152,"startTime":"","endTime":"","selfSupport":0,"pageNum":1,"pageSize":10}
	 * @return
	 */
	@RequestMapping(value = "/queryRevenue", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryRevenue(@RequestBody StoreCenterRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(),model.getPageNum(),model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectRevenueDetails(model)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("营收明细查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * V4.25
	 * 我的钱包界面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/queryJournal", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryTotal(@RequestBody StoreCenterRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(),model.getPageNum(),model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectMyWallet(model)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("我的钱包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * V4.25
	 * 店铺查询
	 * @param model
	 */
	@RequestMapping(value = "/queryStore", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryStore(@RequestBody StoreCenterRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectStoreCenterStudents(model)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("学生店铺查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * V4.25
	 * 店主通讯录
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/queryStudent", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> queryStudent(@RequestBody StoreCenterRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
//			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().selectStoreLink(model.getUserId(),model.getOrderBy())), HttpStatus.OK);
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().queryUnderStudent(model.getUserId(),model.getOrderBy())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("店主通讯录查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * V4.25
	 * @return
	 */
	private StatisticsService getStatisticsService(){
		return statisticsServiceList.get(1);
	}

}
